package com.example.testcomposeproject.widget

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Close
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.testcomposeproject.PreViewData.PreviewPhoneSize
import com.example.testcomposeproject.R
import com.example.testcomposeproject.ui.theme.AppTheme
import com.example.testcomposeproject.ui.theme.cacheAppDimensions

/**
 * @author: hemin
 * @date: 2024/7/16
 * @Desc:
 */
@Preview(showBackground = true, device = PreviewPhoneSize)
@Composable
fun PreViewInputOrChooseItem() {
    AppTheme {
        InputOrChooseItem(onValueChange = {}, onClickChoose = {})
    }
}


/**
 * 输入或选择Item
 */
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun InputOrChooseItem(
    title: String = "灯杆编号",
    hint: String = "输入灯杆编号",
    content: String = "",
    showChooseBtn: Boolean = true,
    onValueChange: (String?) -> Unit,
    onClickChoose: (String) -> Unit = {}
) {
    var text by remember { mutableStateOf(content) }

    Row(
        modifier = Modifier
            .fillMaxWidth()
            .background(MaterialTheme.colorScheme.onPrimary)
            .padding(start = cacheAppDimensions.mediumPadding),
        horizontalArrangement = Arrangement.SpaceBetween,
        verticalAlignment = Alignment.CenterVertically
    ) {
        StarTextView(
            text = title,
        )

        TextField(
            value = text,
            onValueChange = {
                text = it
                onValueChange(it)
            },
            placeholder = { Text(hint) },
            modifier = Modifier
                .weight(1f)
                .padding(end = 8.dp),
            singleLine = true,
            keyboardOptions = KeyboardOptions.Default.copy(
                imeAction = ImeAction.Done
            ),
            keyboardActions = KeyboardActions(
                onDone = {
                }
            ),
            trailingIcon = {
                if (text.isNotEmpty()) {
                    IconButton(onClick = {
                        text = ""
                        onValueChange(null)
                    }
                    ) {
                        Icon(
                            imageVector = Icons.Default.Close,
                            contentDescription = "Clear text"
                        )
                    }
                }
            },
            colors = TextFieldDefaults.textFieldColors(
//                backgroundColor = MaterialTheme.colorScheme.onPrimary,
                focusedIndicatorColor = MaterialTheme.colorScheme.onPrimary,
                unfocusedIndicatorColor = MaterialTheme.colorScheme.onPrimary,
                cursorColor = MaterialTheme.colorScheme.primary
            )
        )

        if (showChooseBtn) {
            Row(
                modifier = Modifier
                    .width(60.dp)
                    .clickable { onClickChoose(text) },
                horizontalArrangement = Arrangement.End,
            ) {
                Icon(
                    painter = painterResource(id = R.drawable.icon_arrow_right_press),
                    contentDescription = null,
                    modifier = Modifier.padding(cacheAppDimensions.mediumPadding)
                )

            }
        }


    }
}
